#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : gtmsAutoTestProject 
@File    : workWeiXinControl.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/4/25 11:19 
@Describe:
"""
import requests
from Comm.tools.toolsControl import ToolsHandler
from Comm.sqlUtils.sqliteControl import SqliteHelper


def send_workWeiXin_msg(post_url, port_name, payload, message, TraceId):
    """
    企业微信推送群聊机器人消息
    :param post_url: 接口地址
    :param port_name: 接口名称
    :param payload: 请求参数
    :param message: 接口响应内容
    :param TraceId: 日志ID
    :return:
    """
    url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=aaa95703-4b94-4f5b-b197-7e7e6519e307'
    now_time = ToolsHandler.get_now_time()
    data = {
        "msgtype": "markdown",
        "markdown": {
            "content": '''<font color=\"info\">{}</font>接口发现报错,请相关同事注意~
                    >报错时间:<font color=\"warning\">**{}**</font> 
                    >接口名称:<font color=\"warning\">**{}**</font> 
                    >请求参数:<font color=\"warning\">`{}`</font> 
                    >接口响应:<font color=\"warning\">**{}**</font> 
                    >TraceId:<font color=\"warning\">`{}`</font>'''.format(post_url, now_time, port_name, payload,
                                                                           message, TraceId)
        }
    }
    requests.post(url, json=data)


def api_error_log_insert(url, types, port_name, payload, response_time, response_code, response_message, TraceId, created_by='system'):
    """
    接口请求失败日志记录
    :param url: 接口地址
    :param types: 所属类型
    :param port_name: 接口名称
    :param payload: 请求参数
    :param response_time: 响应时间
    :param response_code: 响应编码
    :param response_message: 响应数据
    :param TraceId: 日志ID
    :param created_by:
    :return:
    """
    insert_sql = """
    INSERT INTO `api_error_log` (
	url,
	type,
	port_name,
	payload,
	response_time,
	response_code,
	response_message,
	TraceId,
	created_by
)
VALUES
	("{}","{}","{}",'{}',"{}","{}",'{}',"{}","{}");
    """.format(url, types, port_name, payload, response_time, response_code, response_message, TraceId, created_by)
    test = SqliteHelper('support.db')
    test.db_data_operate(insert_sql)
    test.db_close()
